package com.jiepos.mpos.pay.quickpay.service.huiepay;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import com.hy.pmp.client.DefaultQuickPayClient;
import com.hy.utils.JsonUtil;
import com.jiepos.mpos.common.constant.SystemConstant;
import com.jiepos.mpos.core.util.GlobalUtils;
import com.jiepos.mpos.core.util.MathUtils;

public class Req1221Business {
	public static String assembleMsgBody(DefaultQuickPayClient client, Map<String, String> dataMap) throws UnsupportedEncodingException{
        Map<String,Object> kv =new HashMap<String,Object>();
		kv.put("tran_cd", "1221");  //交易类型
		kv.put("prod_cd", GlobalUtils.getPayConfProperty("huiepay.pay.prod_cd")); //产品编码T1:1140  T0:1171
		kv.put("biz_cd", GlobalUtils.getPayConfProperty("huiepay.pay.biz_cd"));  //0000026业务编码
		kv.put("chn_ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.chn_ins_id_cd"));  //密钥机构号
		kv.put("ins_id_cd", GlobalUtils.getPayConfProperty("huiepay.pay.ins_id_cd"));  //机构代码
		kv.put("mcht_cd", GlobalUtils.getPayConfProperty("huiepay.pay.mcht_cd"));  //商户号
		kv.put("tran_dt_tm", dataMap.get("loc_dt") + dataMap.get("loc_ts"));  //订单发送时间
		kv.put("order_id", dataMap.get("loc_trace_no"));  //商户订单号
		kv.put("req_reserved", "");  //请求方保留域
		kv.put("user_id", dataMap.get("trf_mchnt_cd"));  //会员号
		kv.put("curr_cd", "156");  //交易币种
		String tran_amt = String.valueOf(MathUtils.mul(Double.parseDouble(dataMap.get("trans_at")), Double.parseDouble("100")));	
		kv.put("tran_amt", tran_amt.substring(0, tran_amt.indexOf(".")));  //交易金额
		kv.put("order_timeout", GlobalUtils.getPayConfProperty("huiepay.pay.order_timeout"));  //订单支付超时时间
		kv.put("order_desc", "快捷签约+支付");  //订单信息
		
		String pri_acct_no = dataMap.get("act_out_no");   //帐号
		String encryAcctNo = client.encrypt(pri_acct_no, SystemConstant.UTF_ENCODING); //账户信息需要用RSA加密后进行Base64编码	
		//加密银行账号
		kv.put("pri_acct_no", encryAcctNo);
		
		//用户身份敏感信息加密
		Map<String,Object> customer_kv =new HashMap<String,Object>();
		customer_kv.put("cert_id", dataMap.get("certificate_no"));  //证件号码
		customer_kv.put("customer_nm", dataMap.get("real_name"));  //姓名
		customer_kv.put("phone_no", dataMap.get("phone"));  //手机号
		customer_kv.put("sms_code", "");  //短信验证码
		customer_kv.put("cvn2", dataMap.get("cvv2"));  //CVN2
		String month = dataMap.get("exptm").substring(0,2);
		String year = dataMap.get("exptm").substring(2, dataMap.get("exptm").length());
		customer_kv.put("expired",year + month);  //有效期
		customer_kv.put("pin", "");  //持卡人密码
		String customer_json = JsonUtil.toJsonByObj(customer_kv);
		String customer_encrypt = client.encrypt(customer_json, SystemConstant.UTF_ENCODING); //对银行卡验证信息及身份信息加密		
		kv.put("customer_info", customer_encrypt);
		kv.put("noti_url", dataMap.get("payUrl") + GlobalUtils.getPayConfProperty("huiepay.pay.notifyUrl"));  //异步回调地址
		return JsonUtil.toJsonByObj(kv);
	}
}
